from redis import Redis

cache = Redis(host='192.168.242.9',port=6379,password='root')

# 1.操作字符串
# cache.set('username','abox')
# print(cache.get('username'))
# cache.delete('username')

# # 2.列表的操作
# cache.lpush('languages','java')
# cache.lpush('languages','python')
# cache.lpush('languages','php')

# # 3.集合的操作
# cache.sadd('team','li')
# cache.sadd('team','huang')
# cache.sadd('team','zhang')
# print(cache.smembers('team'))

# # 哈希操作
# cache.hset('website','baidu','www.baidu.com')
# cache.hset('website','google','www.google.com')

# # 事务
# pip = cache.pipeline()
# pip.set('username','zhiliao')
# pip.set('password','11111')
# pip.execute()

# 发布与订阅功能
# 异步发送邮件的功能
ps = cache.pubsub()
ps.subscribe('email')
while True:
    for item in ps.listen():
        # print(item)
        if item['type'] == 'message':
            data = item['data']
            print(data)